package com.wyx.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wyx.pojo.Article;
import com.wyx.pojo.MyPage;
import com.wyx.pojo.Result;
import com.wyx.pojo.User;
import com.wyx.service.ArticleService;
import com.wyx.service.UserService;
import com.wyx.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody @Validated Article article) {
        Map<String, Object> map = ThreadLocalUtil.get();
        article.setCreateUser((int) map.get("id"));
        return Result.success(articleService.insertArticle(article));
    }

    @GetMapping("/querySting")
    public Result querySting(Integer pageNum, Integer pageSize,
                             @RequestParam(required = false)/*非必须参数*/ Integer categoryId,
                             @RequestParam(required = false) String state) {
        Article article = new Article();
        article.setState(state);
        Map<String, Object> map = ThreadLocalUtil.get();
        article.setCreateUser((Integer) map.get("id"));
        PageHelper.startPage(pageNum, pageSize);
        Page<Article> page = (Page<Article>) articleService.selectArticleByAll(article);
        return Result.success(new MyPage<>(page.getTotal(), page.getResult()));
    }

    @GetMapping("myArticle")
    public Result myArticle(Integer pageNum, Integer pageSize) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Article article = new Article();
        System.out.println(article);
        article.setCreateUser((Integer) map.get("id"));
        PageHelper.startPage(pageNum, pageSize);
        Page<Article> page = (Page<Article>) articleService.selectArticleByAll(article);
        return Result.success(new MyPage<>(page.getTotal(), page.getResult()));

    }

    @PostMapping("browse")
    public Result browse(Integer num, Integer size) {
        PageHelper.startPage(num, size);
        Page<Map<String, Object>> page = (Page<Map<String, Object>>) articleService.browseArticle();
        return Result.success(new MyPage<>(page.getTotal(), page.getResult()));
    }

    @PostMapping("/stagingArticle")
    public Result stagingArticle(@RequestBody Article article) {
        System.out.println(article);
        if (article.getId() == null || article.getId() == 0) {
            Map<String, Object> map = ThreadLocalUtil.get();
            article.setCreateUser((Integer) map.get("id"));
            articleService.insertArticle(article);
            article = articleService.selectArticleByAll(article).get(0);
        } else {
            articleService.updateArticleById(article);
        }
        return Result.success(article.getId());
    }

    @GetMapping("/query")
    public Result query(Integer data) {
        System.out.println("query:");
        System.out.println(data);
        Article article = new Article();
        article.setId(data);
        System.out.println(articleService.selectArticleByAll(article));
        return Result.success(articleService.selectArticleByAll(article).get(0));
    }

    @GetMapping("/delete")
    public Result delete(int id) {
        return Result.success(articleService.deleteArticleById(id));
    }

    @PostMapping("/queryAuthorArticle")
    public Result queryAuthorArticle(Integer data, Integer num, Integer size) {
        Article article = new Article();
        article.setCreateUser(data);
        article.setState("发布");
        PageHelper.startPage(num, size);
        Page<Article> page = (Page<Article>) articleService.selectArticleByAll(article);
        return Result.success(new MyPage<>(page.getTotal(), page.getResult()));
    }
}
